package com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktakingList;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktakingList.vo.EquipmentStocktakingListPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktakingList.vo.EquipmentStocktakingListPageResp;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.enums.EquipmentStatus;
import com.xiaoshuidi.cloud.module.rooms.service.IEquipmentStocktakingListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**
 * 资产管理-盘点管理
 *
 * @author Link
 * @since 2023-05-21
 */
@Tag(name = "管理后台 - 盘点清单管理")
@RestController
@RequestMapping(ApiConstants.NAME + "/equipmentStocktakingList")
@RequiredArgsConstructor
public class EquipmentStocktakingListController {

    @Resource
    private IEquipmentStocktakingListService iEquipmentStocktakingListService;

    /**
     * 盘点明细-分页查询
     */
    @GetMapping("/page")
    @Operation(summary = "盘点明细-分页查询", description = "若位置为空，页面展示：公寓公区")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:stocktaking:list:query')")
    public CommonResult<PageResult<EquipmentStocktakingListPageResp>> getEquipmentStocktakingListPage(@Valid EquipmentStocktakingListPageReqVO pageVO) {
        IPage<EquipmentStocktakingListPageResp> result = iEquipmentStocktakingListService.getEquipmentStocktakingListPage(pageVO);

        List<EquipmentStocktakingListPageResp> list = result.getRecords();
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(s -> s.setStatus(EquipmentStatus.getNameByCode(s.getStatus())));
        }
        PageResult<EquipmentStocktakingListPageResp> ret = new PageResult<>();
        ret.setList(list);
        ret.setTotal(result.getTotal());
        return success(ret);
    }


}
